<?php defined('BASEPATH') or exit('No direct script access allowed');

class Cricket_m extends MY_Model {	
	function __construct(){
		parent::__construct();
	}
	
	function advertising(){
		$id = intval($_GET['id']);
		$ADV = $this->advertising_entity->init('id',$id);
		$ADV->setClick($ADV->getClick()+1);
		$ADV->update();
		redirect($ADV->getUrl());
		exit();
	}
	
	function convertionBuild(){
		$rate_convertion = rateConvertion(($_GET['mid']));
		$MATCH = $this->match_entity->init('id',$_GET['mid']);
		$rate_parse = explode(':',$rate_convertion);
		$pointRate = $rate_parse[0];
		$coinRate = $rate_parse[1];
		
		header("content-type:application/x-javascript");
		echo "sysconvertion = {point:{$pointRate},coin:{$coinRate}};";
		if($MATCH AND $MATCH->getEnableConvertion() != 'yes'){
			echo "convertEnable = 'no';";
		}else{
			echo "convertEnable = 'yes';";
		}
		exit();		
	}
	 
	function bet_history($bet_type,$type,$mode,$fromDate,$toDate){
		$USER = $this->user_entity->init('id',getUserId());
		$typeOption = array('settled'=> 'Settled Bets','unsettled'=> 'Unsettled Bets');
		$betTypeArray = getPredictTypeDataConfig();
	
		$bet_type = in_array($bet_type,array_keys($betTypeArray)) ? $bet_type : 'predefined' ;
		$type = in_array($type,array_keys($typeOption)) ? $type:'settled';
		$mode = in_array($mode,array('m1','m2')) ? $mode:'m1';
		$fromDate = $fromDate;
		$toDate = $toDate;
		
		$fromDateStamp = convertToTimeStampFromFormat(  $fromDate,"d/m/Y" );
		$toDateStamp   = convertToTimeStampFromFormat( $toDate,"d/m/Y" );
		
		if($mode == 'm1'){
			$toDateStamp = time();
			$fromDateStamp = $toDateStamp - 86400 - 19800;
		}
		if($type == 'settled'){
			if($bet_type == 'predefined'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET." WHERE user_id=".$USER->getId()." AND status NOT LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC")
					->result();
			}else if($bet_type == 'manual'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE user_id=".$USER->getId()." AND status NOT LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC")
					->result();
			}else if($bet_type == 'tournament'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE user_id=".$USER->getId()." AND status NOT LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC")
					->result();
			}else if($bet_type == 'manual_tournament'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE user_id=".$USER->getId()." AND status NOT LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC")
					->result();
			}			
		}else{
			if($bet_type == 'predefined'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET." WHERE user_id=".$USER->getId()." AND status LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC") //GROUP BY match_id
					->result();
			}else if($bet_type == 'manual'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE user_id=".$USER->getId()." AND status LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC") //GROUP BY match_id
					->result();
			}else if($bet_type == 'tournament'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE user_id=".$USER->getId()." AND status LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC") //GROUP BY match_id
					->result();
			}else if($bet_type == 'manual_tournament'){
				$result = $this->db
					->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE user_id=".$USER->getId()." AND status LIKE 'betting' AND bet_time>= $fromDateStamp AND bet_time<= $toDateStamp ORDER BY bet_time DESC") //GROUP BY match_id
					->result();
			}				
		}
		return $result;
	}
	
	function convertion_history(){
		$USER = $this->user_entity->init('id',getUserId());
		$mode = in_array($this->input->get('mode',FALSE),array('m1','m2')) ? $this->input->get('mode',FALSE):'m1';
		$fromDate = $this->input->get('fromDate',FALSE);
		$toDate = $this->input->get('toDate',FALSE);
		
		$fromDateStamp = convertToTimeStampFromFormat(  $fromDate,"d/m/Y" );
		$toDateStamp   = convertToTimeStampFromFormat( $toDate,"d/m/Y" );
		
		if($mode == 'm1'){
			$toDateStamp = time();
			$fromDateStamp = $toDateStamp - 86400 - 19800;
		}
		
		$result = $this->db
						->query("SELECT * FROM ".TBL_CONVERSION." WHERE user_id=".$USER->getId()." AND time>= $fromDateStamp AND time<= $toDateStamp ORDER BY time DESC") //GROUP BY match_id
						->result();
		
		return $result;
	}
	
	function my_cod_orders(){
		$USER = $this->user_entity->init('id',getUserId());
		$mode = in_array($this->input->get('mode',FALSE),array('m1','m2')) ? $this->input->get('mode',FALSE):'m1';
		$fromDate = $this->input->get('fromDate',FALSE);
		$toDate = $this->input->get('toDate',FALSE);
		
		$fromDateStamp = convertToTimeStampFromFormat(  $fromDate,"d/m/Y" );
		$toDateStamp   = convertToTimeStampFromFormat( $toDate,"d/m/Y" );
		
		if($mode == 'm1'){
			$toDateStamp = time();
			$fromDateStamp = $toDateStamp - 86400 - 19800;
		}
		
		$result = $this->db
						->query("SELECT * FROM ".TBL_COD." WHERE user_id=".$USER->getId()." AND created_time>= $fromDateStamp AND created_time<= $toDateStamp ORDER BY created_time DESC") //GROUP BY match_id
						->result();
		
		return $result;
	}
	
	function creditsAddToCoins(){
		$username = $this->input->get('username', TRUE);
		$sid = $this->input->get('sid',TRUE);
		$credits = $this->input->get('credits',TRUE);
		
		$USER = $this->user_entity->init('username', $username);
		if(!$USER){
			$array['result'] = 'error';
			$array['message'] = 'User not found';
			$array['coins_return'] = null;
			
			$string = json_encode($array);
			echo $string;
			exit();
		}
		
		if($USER){
			if($USER->getSid() != $sid OR ! strlen($sid) ){
				$array['result'] = 'error';
				$array['message'] = 'Sid error';
				$array['coins_return'] = null;
				
				$string = json_encode($array);
				echo $string;
				exit();
			}
		}
		
		if(! $this->phpvalidator->is_numeric($credits) ){
			$array['result'] = 'error';
			$array['message'] = 'Credits error';
			$array['coins_return'] = null;
			
			$string = json_encode($array);
			echo $string;
			exit();
		}
		
		$obj = $this->auction_coupon->findObjBaseCredit($credits);
		if(!$obj){
			$array['result'] = 'error';
			$array['message'] = 'Credits not found';
			$array['coins_return'] = null;
			
			$string = json_encode($array);
			echo $string;
			exit();
		}
		
		 
		$coins = $obj->coins;
		$startTime= time();
		$toTime = time() + 86400 * intval($obj->days_accept);
		
		$couponcode = $this->digit->rand_string(12);
		
		$newid = $this->coupon_entity->createNewEntity();
		$COUPON = $this->coupon_entity->init('id', $newid);
		
		$COUPON->setCode($couponcode);
		$COUPON->setValidity('pending');
		$COUPON->setCoin($coins);
		$COUPON->setUserRedeem(NULL);
		$COUPON->setStartDate($startTime);
		$COUPON->setEndDate($toTime);
		$COUPON->setCreateDate(time());
		$COUPON->setMode('single');
		$COUPON->setIsAuction('yes');
		$COUPON->setUsername($username);
		$COUPON->setCredits($credits);
		 
		$COUPON->update();
		$this->email_sender->_sendEmailToUser_couponcodeFromAuctionAddCredits($newid);
		
		$array['result'] = 'success';
		$array['message'] = 'Success';
		$array['coins_return'] = $coins;
		$array['coupon_id'] = $COUPON->getId();
		
		$USER->setSid($this->module_helper->getTokenId());
		$USER->update();
	
		$string = json_encode($array);
		echo $string;
		exit();
		//header('Content-type: text/json');
		
	}
	
	
	function contact_us_submit(){
		extract($this->input->post());
		
		if($_SESSION['captcha'] != $captcha){
			echo json_encode( array('result'=>'err','msg'=>'Security code not match') );
			exit();			
		}
		if(strlen($name) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Name must be fill') );
			exit();			
		}
		if(!$this->phpvalidator->is_email($email)){
			echo json_encode( array('result'=>'err','msg'=>'Email not valid') );
			exit();			
		}
		if(strlen($subject) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Subject must be fill') );
			exit();			
		}
		if(strlen($message) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Message must be fill') );
			exit();			
		}
		
		$name = substr($name,0,45);
		$subject= substr($subject,0,145 );
		$message = substr($message,0,550);
		
		$header = "From: $email <br/>
					Contact name: $name <br/> 
					Contact Mobile number: $mobile <br/>
					Subject: $subject <br/>";
		
		if( ENVIRONMENT != 'local' ){
			$contactServerEmail  ='info@clicxa.com';
		}else{
			$contactServerEmail  ='joss3js@gmail.com';
		}	
		
			
		$this->email_sender->setFromEmail($email);
		$this->email_sender->setToEmail($contactServerEmail);
		$this->email_sender->setSubject($subject);
		$this->email_sender->setBody($header.'<br/>'.$message);
		$this->email_sender->setEmailTitle($email);
		
		$this->email_sender->sendEmail();
		
		echo json_encode( array('result'=>'ok','msg'=>'') );//Thanks for your contact.
		exit();
	}
	
	function contact_help_submit(){
		extract($this->input->post());
		
		if($_SESSION['captcha'] != $captcha){
			echo json_encode( array('result'=>'err','msg'=>'Security code not match') );
			exit();			
		}
		if(strlen($name) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Name must be fill') );
			exit();			
		}
		if(!$this->phpvalidator->is_email($email)){
			echo json_encode( array('result'=>'err','msg'=>'Email not valid') );
			exit();			
		}
		if(strlen($subject) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Subject must be fill') );
			exit();			
		}
		if(strlen($message) == 0){
			echo json_encode( array('result'=>'err','msg'=>'Message must be fill') );
			exit();			
		}
		
		$name = substr($name,0,45);
		$subject= substr($subject,0,145 );
		$message = substr($message,0,550);
		
		$header = "From: $email <br/>
				Contact name: $name <br/> 
				Contact Mobile number: $mobile <br/> 
				Type Of Issue: {$issue_type} <br/>
				Subject: $subject <br/>
				";
		if( ENVIRONMENT != 'local' ){
			$contactServerEmail  ='support@clicxa.com';
		}else{
			$contactServerEmail  ='joss3js@gmail.com';
		}	
			
		$this->email_sender->setFromEmail($email);
		$this->email_sender->setToEmail($contactServerEmail);
		$this->email_sender->setSubject($subject);
		$this->email_sender->setBody($header.'<br/>'.$message);
		$this->email_sender->setEmailTitle($email);
		
		$this->email_sender->sendEmail();
		
		echo json_encode( array('result'=>'ok','msg'=>'') );
		exit();
	}
	
	
	
	
//end class	
}